<script setup lang="ts">
import { ref, toRefs } from 'vue';
import {
    formatTime,
} from "@/renderer/utils/helper.ts";
import { ipcRenderer } from 'electron';
import html2canvas from 'html2canvas';
import "@/renderer/window/WindowMain/custom-wangeditor.css"; // 引入 css
import MyEditor from "@/renderer/components/MyEditor.vue";



const MyEditorRef = ref()
const emits = defineEmits(['handelSubmit', 'update:isEditing'])
const props = defineProps<{
    currentNote: any,
    isEditing: boolean,
}>()

const { currentNote, isEditing } = toRefs(props)

const handelSubmit = async () => {
    emits('handelSubmit', currentNote.value, 'content')
}

const setEditing = () => {
    emits('update:isEditing', true)
}
const saveContentPng = async () => {
    // 获取要转换的DOM元素
    const element = document.getElementById('editor-content-view');
    // 使用html2canvas生成canvas
    html2canvas(element!, {
        allowTaint: true,
        scale: 2, // 提升画面质量，但是会增加文件大小
        useCORS: true, // 允许使用CORS资源
    }).then(async (canvas: { toDataURL: (arg0: string) => any; }) => {
        // 将canvas转换为图片
        const imgData = canvas.toDataURL('image/png');

        // 显示打开文件对话框
        const { filePath } = await ipcRenderer.invoke("showSaveDialog", {
            title: "下载图片",
            defaultPath: `${currentNote.value.title ?? 'screenshot'}.png`,
            filters: [
                { name: "All Files", extensions: ["*"] },
            ],
        });
        // 在Electron中保存图片
        ipcRenderer.invoke('saveImage', { imgData, filePath });
    });
};

const getContext = () => {
    return MyEditorRef.value.getContext();
};
defineExpose({ getContext });

</script>

<template>


    <div  class="content-title" v-if="!currentNote.isFolder" >
        {{ currentNote.title }}
    </div>
    <div class="content-container">
        <MyEditor ref="MyEditorRef" v-if="isEditing" class="editor-wrapper-refs-class" :content="currentNote.content" />
        <div v-else-if="!isEditing&&!currentNote.content" class="empty-content-container">
            <div class="logo-placeholder">
                <span class="iconfont icon-notes logo-icon"></span>
                <div class="logo-text">记忆笔记</div>
                <div class="logo-subtext">记录每一个重要时刻</div>
            </div>
        </div>
        <div v-else class="editor-content-view" v-highlight>
            <div id="editor-content-view" v-html="currentNote.content"></div>
        </div>
        <div class="editor-toolbar" v-show="currentNote.id && !currentNote.isFolder">
            <span class="update-time">最近更新时间：{{
                currentNote.updated && formatTime(currentNote.updated)
            }}</span>
            <div class="editor-actions">
                <a-button size="small" class="btn-download" :disabled="isEditing"
                    @click="saveContentPng">下载</a-button>
                <a-button v-if="isEditing" type="primary" size="small" class="btn-save" @click="handelSubmit()">保存</a-button>
                <a-button v-else size="small" type="primary" class="btn-edit" @click="setEditing()">编辑</a-button>
            </div>
        </div>
    </div>

</template>

<style scoped>
.content-title {
    flex: 1 0 0px;
    font-size: 24px;
    line-height: 40px;
    text-align: center;
    padding: 0 120px;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
    color: var(--text-primary);
    margin: 10px 0;
}

.content-container {
    display: flex;
    flex-direction: column;
    padding: 0 15px;
    height: calc(100% - 50px);
}

 .editor-wrapper-refs-class {
    flex: 1;
    min-height: 0; /* 防止内容撑开父容器 */
}

.editor-content-view {
    flex: 1;
    font-size: 14px;
    padding: 15px !important;
    line-height: 22px;
    overflow-y: auto;
    width: 100%;
    white-space: pre-wrap;
    word-break: break-all;
    background-color: var(--surface-color);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
    min-height: 0; /* 防止内容撑开父容器 */
}

.editor-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    background-color: var(--surface-color);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
    margin: 10px 0;
}

.update-time {
    font-size: 13px;
    color: var(--text-placeholder);
}

.editor-actions {
    display: flex;
    gap: 10px;
}

.btn-download {
    margin-right: 15px;
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
    background-color: var(--surface-color);
    color: var(--text-primary);
    transition: var(--transition);
}

.btn-download:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.btn-save {
    border-radius: var(--border-radius);
    border: 1px solid var(--primary-color);
    background-color: var(--primary-color);
    color: white;
    transition: var(--transition);
}

.btn-save:hover {
    background-color: var(--primary-hover-color);
    border-color: var(--primary-hover-color);
    box-shadow: var(--shadow);
}

.btn-edit {
    border-radius: var(--border-radius);
    border: 1px solid var(--primary-color);
    background-color: var(--primary-color);
    color: white;
    transition: var(--transition);
}

.btn-edit:hover {
    background-color: var(--primary-hover-color);
    border-color: var(--primary-hover-color);
    box-shadow: var(--shadow);
}

/* 空内容占位符样式 */
.empty-content-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 0;
    position: relative;
    overflow: hidden;
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
}

.empty-content-container::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: 
        radial-gradient(circle at 10% 20%, rgba(236, 242, 250, 0.6) 0%, transparent 20%),
        radial-gradient(circle at 90% 80%, rgba(236, 242, 250, 0.6) 0%, transparent 20%);
    z-index: 1;
}

.logo-placeholder {
    text-align: center;
    z-index: 2;
    padding: 40px;
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.85);
    box-shadow: 
        0 4px 20px rgba(135, 150, 165, 0.15),
        0 1px 0 rgba(255, 255, 255, 0.9) inset;
    border: 1px solid rgba(230, 235, 240, 0.8);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.logo-icon {
    font-size: 72px;
    color: #5a8fce;
    margin-bottom: 20px;
    display: block;
    text-shadow: 0 2px 4px rgba(90, 143, 206, 0.1);
}

.logo-text {
    font-size: 24px;
    font-weight: 500;
    color: #3a5a7d;
    margin-bottom: 10px;
    letter-spacing: 0.5px;
}

.logo-subtext {
    font-size: 14px;
    color: #7d8f9f;
    font-weight: 300;
}
</style>